// 1. 引入要用到的api
import { defineStore } from "pinia"
import type { CategoryStateModel } from "@/api/product/model/category"
// 引入函数
import {
  reqGetCategory1List,
  reqGetCategory2List,
  reqGetCategory3List,
} from "@/api/product/category"

// 创建模块对象
export const useGetCategoryStore = defineStore("categoryList", {
  state: (): CategoryStateModel => {
    return {
      // 用于收集id
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      //   用于存储响应回来的分类数据
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },
  actions: {
    // 获取一级分类数据
    async getCategory1ListData() {
      const result = await reqGetCategory1List()
      //   console.log(result[0])
      this.category1List = result
    },

    // 获取二级分类数据
    async getCategory2ListData(id: number) {
      this.category2Id = undefined
      this.category3Id = undefined
      this.category1Id = id
      const result = await reqGetCategory2List(id)
      //   console.log(result)

      this.category2List = result
    },

    // 获取三级分类数据
    async getCategory3ListData(id: number) {
      this.category3Id = undefined
      this.category2Id = id
      const result = await reqGetCategory3List(id)
      //   console.log(result[0])

      this.category3List = result
    },

    // 保存三级id
    setCategory3Id(id: number) {
      this.category3Id = id
    },
  },
})
